from django.shortcuts import render, HttpResponse, redirect
from django.views import View
from django.views.decorators import csrf

from iClouds.allSerializers import *


def logout(request):
    # del request.session["is_login"] # 删除session_data里的一组键值对
    request.session.flush()  # 删除一条记录包括(session_key session_data expire_date)三个字段
    return redirect("/admin/login/")


class Login(View):
    def get(self, request):
        return render(request, "admin/login.html")

    def post(self, request):
        ctx = {}
        user = request.POST.get("email")
        pwd = request.POST.get("pswd")
        # print(user, pwd)
        try:
            r = AllinpayAuthAdmin.objects.get(
                Q(username=user) | Q(phone=user) | Q(email=user)
            )
        except BaseException as err:
            ctx["rlt"] = f"{err}"
            return render(request, "admin/login.html", ctx)
        if not r.password == pwd:
            ctx["rlt"] = "Password is err"
            return render(request, "admin/login.html", ctx)
        if r.is_super:
            r.lasttime = timezone.now()
            r.save()
            request.session["is_login"] = True
            request.session["user1"] = r.id
            request.session["user2"] = r.nicename
            request.session["user3"] = r.is_super
            return redirect("/admin/index/")
        if not r.status:
            ctx["rlt"] = "Status is err"
            return render(request, "admin/login.html", ctx)
        if not r.is_authenticated:
            ctx["rlt"] = "Authenticated is err"
            return render(request, "admin/login.html", ctx)
        else:
            r.lasttime = timezone.now()
            r.save()
            # status = request.session.get('is_login')
            request.session["is_login"] = True
            request.session["user1"] = r.id
            request.session["user2"] = r.nicename
            request.session["user3"] = r.is_super
            return redirect("/admin/index/")


class Index(View):
    def get(self, request):
        login = request.session.get("is_login")
        root = {
            "id": request.session.get("user1"),
            "nicename": request.session.get("user2"),
            "is_super": request.session.get("user3"),
        }
        if not login:
            return redirect("/admin/logout/")
        return render(request, "admin/index.html", root)

    def post(self, request):
        return HttpResponse("POST")


class Root(View):
    def get(self, request):
        login = request.session.get("is_login")
        root = {
            "id": request.session.get("user1"),
            "nicename": request.session.get("user2"),
            "is_super": request.session.get("user3"),
        }
        if not login:
            return redirect("/admin/logout/")

        # 查询当前身份
        try:
            answer = AllinpayAuthAdmin.objects.get(id=root["id"])
        except BaseException as err:
            return redirect("/admin/logout/")

        # 查询管理员列表
        if answer.is_super:
            r = AllinpayAuthAdmin.objects.exclude(id=1)
        else:
            r = AllinpayAuthAdmin.objects.filter(id=root["id"])
        s = ModelAllinpayAuthAdminSerializer(r, many=True)
        return render(request, "admin/root.html", {"root": s.data})

    def post(self, request):
        login = request.session.get("is_login")
        root = {
            "id": request.session.get("user1"),
            "nicename": request.session.get("user2"),
            "is_super": request.session.get("user3"),
        }
        if not login:
            return redirect("/admin/logout/")

        # 提取数据
        act = request.POST.get("act")
        if act == "pwsd":
            idx = request.POST.get("idx")
            pswd = request.POST.get("pswd")
            try:
                r = AllinpayAuthAdmin.objects.get(id=int(idx))
                r.password = pswd
                r.save()
            except BaseException as err:
                return HttpResponse(f"{err}")
        return HttpResponse("success")
